/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelModificarUsuario.java
 *
 * Created on 10/04/2011, 10:07:56 PM
 */

package frontera.interfaz.panel;

import control.SIRHApp;
import entidad.Usuario;
import frontera.interfaz.panel.herramientas.PanelHerramientasBuscar;

/**
 *
 * @author Sebastian
 */
public class PanelModificarUsuario extends javax.swing.JPanel {

    /** Creates new form PanelModificarUsuario */
    public PanelModificarUsuario(Usuario modificarUsuario) {
        initComponents();
        inicializarVariables(modificarUsuario);
    }

    private void inicializarVariables(Usuario modificarUsuario){
        labelUsuario.setText(modificarUsuario.getNombre());
        labelEmpleado.setText(modificarUsuario.getEmpleadoAsociado().getNombre());
        labelPermisos.setText(modificarUsuario.getEmpleadoAsociado().getCargoAsociado().getNivelPermisos());
        claveTF.setText("");
        claveVerTF.setText("");
        usuario = modificarUsuario;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelDatosBasicos = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        claveTF = new javax.swing.JPasswordField();
        labelSalarioBasico = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        labelSalarioBasico1 = new javax.swing.JLabel();
        claveVerTF = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        botonGuardar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        labelResultado = new javax.swing.JLabel();
        labelUsuario = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        labelPermisos = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        labelEmpleado = new javax.swing.JLabel();

        panelDatosBasicos.setBorder(javax.swing.BorderFactory.createTitledBorder("Cambiar Contraseña"));
        panelDatosBasicos.setLayout(new java.awt.GridLayout(0, 1));

        claveTF.setText("ValorAntiguo");

        labelSalarioBasico.setText("Contraseña");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelSalarioBasico, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(claveTF, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSalarioBasico)
                    .addComponent(claveTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        panelDatosBasicos.add(jPanel1);

        labelSalarioBasico1.setText("Verificar Contraseña");

        claveVerTF.setText("ValorAntiguo");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelSalarioBasico1)
                .addGap(18, 18, 18)
                .addComponent(claveVerTF, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSalarioBasico1)
                    .addComponent(claveVerTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        panelDatosBasicos.add(jPanel2);

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 32));
        jLabel2.setText("Editando Usuario");

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Save.png"))); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Cancelar.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/UpdUsuarios.png"))); // NOI18N

        labelUsuario.setFont(new java.awt.Font("Dialog", 0, 22));

        jLabel3.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel3.setText("Nivel De Permisos: ");

        labelPermisos.setFont(new java.awt.Font("Dialog", 0, 14));

        jLabel4.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel4.setText("Empleado Asociado: ");

        labelEmpleado.setFont(new java.awt.Font("Dialog", 0, 14));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(labelEmpleado, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(labelPermisos, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE))
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)))
                    .addComponent(panelDatosBasicos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 676, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 428, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(botonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCancelar))
                    .addComponent(labelResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 653, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))))
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(labelPermisos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelDatosBasicos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCancelar)
                    .addComponent(botonGuardar))
                .addContainerGap(144, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed

    
        String resultadoOperacion = SIRHApp.getInstance().getAdmUsuarios().actualizarUsuario(usuario, String.valueOf(claveTF.getPassword()));

        if ( String.valueOf(claveTF.getPassword()).equals(String.valueOf(claveVerTF.getPassword()))){

            if(resultadoOperacion.equals("")){
                SIRHApp.getInstance().getVentanaPrincipal().getPanelUsuarios().actualizarLista(SIRHApp.getInstance().getSistema().getUsuarios());
                SIRHApp.getInstance().getVentanaPrincipal().setPanelUsuariosPrincipal( SIRHApp.getInstance().getVentanaPrincipal().getPanelUsuarios());
                ((PanelHerramientasBuscar<Usuario>) SIRHApp.getInstance().getFactoryInterfaz().getPanelUsuarios().getPanelHerramientas()).inicializarBusqueda();
            }
            else{
                    labelResultado.setText(resultadoOperacion);
            }
        }

        else{
                labelResultado.setText("Las contraseñas no coinciden. Por favor verifique que haya digitado correctamente.");
        }
}//GEN-LAST:event_botonGuardarActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        SIRHApp.getInstance().getVentanaPrincipal().setPanelUsuariosPrincipal(SIRHApp.getInstance().getVentanaPrincipal().getPanelUsuarios());
        ((PanelHerramientasBuscar<Usuario>) SIRHApp.getInstance().getFactoryInterfaz().getPanelUsuarios().getPanelHerramientas()).inicializarBusqueda();
}//GEN-LAST:event_botonCancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JPasswordField claveTF;
    private javax.swing.JPasswordField claveVerTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel labelEmpleado;
    private javax.swing.JLabel labelPermisos;
    private javax.swing.JLabel labelResultado;
    private javax.swing.JLabel labelSalarioBasico;
    private javax.swing.JLabel labelSalarioBasico1;
    private javax.swing.JLabel labelUsuario;
    private javax.swing.JPanel panelDatosBasicos;
    // End of variables declaration//GEN-END:variables
    private Usuario usuario;

}
